package zhr.chapter13.zuoye;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class client {
    public static void main(String[] args) throws IOException {
        new client.TCPClient().connect();
    }
    static class TCPClient {
        private static final int PORT = 7788;

        public void connect() throws IOException {
            Socket client = new Socket(InetAddress.getLocalHost(), PORT);
            InputStream is = client.getInputStream();
            OutputStream bos = client.getOutputStream();

            byte[] buf = new byte[1024];
            int len = is.read(buf);
            System.out.println(new String(buf, 0, len)); // 打印服务器的欢迎消息

            Scanner scanner = new Scanner(System.in);
            System.out.println("请分别输入三条边:");
            double a = scanner.nextDouble();
            double b = scanner.nextDouble();
            double c = scanner.nextDouble();
            String datasend = a + "," + b + "," + c;
            System.out.println("向服务器发送内容: " + datasend);
            bos.write(datasend.getBytes());
            bos.write("\n".getBytes()); // 再次添加换行符
            bos.flush(); // 清空缓冲区，确保数据发送

            // 接收服务器的计算结果
            len = is.read(buf);
            System.out.println("服务器返回的结果: " + new String(buf, 0, len));

            // 关闭资源
            bos.close();
            client.close();
            scanner.close();
        }
    }
}